package com.dms.modules.product.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dms.modules.product.entity.ProductRecommendationMetrics;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品推荐指标Mapper接口
 */
@Mapper
public interface ProductRecommendationMetricsMapper extends BaseMapper<ProductRecommendationMetrics> {
    
    @Select("SELECT * FROM product_recommendation_metrics WHERE experiment_id = #{experimentId}")
    List<ProductRecommendationMetrics> getMetricsByExperimentId(Long experimentId);
    
    @Update("UPDATE product_recommendation_metrics SET click_count = click_count + 1, " +
            "update_time = NOW() WHERE user_id = #{userId} AND " +
            "recommended_product_ids LIKE CONCAT('%', #{productId}, '%')")
    void incrementClickCount(@Param("userId") Long userId, @Param("productId") Long productId);
    
    @Update("UPDATE product_recommendation_metrics SET view_count = view_count + 1, " +
            "update_time = NOW() WHERE user_id = #{userId} AND " +
            "recommended_product_ids LIKE CONCAT('%', #{productId}, '%')")
    void incrementViewCount(@Param("userId") Long userId, @Param("productId") Long productId);
    
    @Select("SELECT AVG(click_count * 1.0 / view_count) FROM product_recommendation_metrics " +
            "WHERE recommendation_type = #{recommendationType} AND experiment_group = #{experimentGroup}")
    double calculateCTR(@Param("recommendationType") String recommendationType, 
            @Param("experimentGroup") String experimentGroup);
    
    @Select("SELECT AVG(stay_duration) FROM product_recommendation_metrics " +
            "WHERE recommendation_type = #{recommendationType} AND experiment_group = #{experimentGroup}")
    double calculateAverageStayTime(@Param("recommendationType") String recommendationType, 
            @Param("experimentGroup") String experimentGroup);
} 